const mysql = require('mysql')
//数据库配置
const conn = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: 'klren0312',
  database: 'rain',
  port: 3306
})
//连接数据库
conn.connect()
let i = 0
let interval = setInterval(() => {
  i++
  if (i === 5) {
    clearInterval(interval)
  }
  const temO = {
    name: 'tem',
    value: Math.floor(Math.random() * (i * 10)  + 10)
  }
  const humO = {
    name: 'hum',
    value: Math.floor(Math.random() * (i * 10)  + 20)
  }
  conn.query('INSERT INTO sensor SET ?', temO, function(error, result, fields) {
    if (error) throw error
  })
  conn.query('INSERT INTO sensor SET ?', humO, function(error, result, fields) {
    if (error) throw error
  })
}, 2000)
